package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.eiahe.hr.salary.domain.bo.CashPaymentBo;
import org.eiahe.hr.salary.domain.vo.CashPaymentVo;
import org.eiahe.hr.salary.service.ICashPaymentService;
import org.ehe.common.excel.utils.ExcelUtil;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 现金发放Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/cash")
@Slf4j
public class CashPaymentController extends BaseController {

    private final ICashPaymentService iCashPaymentService;

    /**
     * 查询现金发放列表
     */
    @SaCheckPermission("salary:cash:list")
    @GetMapping("/list")
    public TableDataInfo<CashPaymentVo> list(CashPaymentBo bo, PageQuery pageQuery) {
        return iCashPaymentService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取现金发放详细信息
     *
     * @param paymentId 主键
     */
    @SaCheckPermission("salary:cash:query")
    @GetMapping("/{paymentId}")
    public R<CashPaymentVo> getInfo(@PathVariable Long paymentId) {
        return R.ok(iCashPaymentService.queryById(paymentId));
    }

    /**
     * 新增现金发放
     */
    @SaCheckPermission("salary:cash:add")
    @Log(title = "现金发放", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CashPaymentBo bo) {
        return toAjax(iCashPaymentService.insertByBo(bo));
    }

    /**
     * 修改现金发放
     */
    @SaCheckPermission("salary:cash:edit")
    @Log(title = "现金发放", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CashPaymentBo bo) {
        return toAjax(iCashPaymentService.updateByBo(bo));
    }

    /**
     * 删除现金发放
     *
     * @param paymentIds 主键串
     */
    @SaCheckPermission("salary:cash:remove")
    @Log(title = "现金发放", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paymentIds}")
    public R<Void> remove(@PathVariable Long[] paymentIds) {
        return toAjax(iCashPaymentService.deleteWithValidByIds(Arrays.asList(paymentIds)));
    }

    /**
     * 导出现金发放列表
     */
    @SaCheckPermission("salary:cash:export")
    @Log(title = "现金发放", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CashPaymentBo bo, HttpServletResponse response) {
        List<CashPaymentVo> list = iCashPaymentService.queryList(bo);
        ExcelUtil.exportExcel(list, "现金发放数据", CashPaymentVo.class, response);
    }

    /**
     * 领取现金
     *
     * @param paymentId 现金发放ID
     */
    @SaCheckPermission("salary:cash:receive")
    @Log(title = "现金发放", businessType = BusinessType.UPDATE)
    @PutMapping("/receive/{paymentId}")
    public R<Void> receiveCash(@PathVariable Long paymentId) {
        return toAjax(iCashPaymentService.receiveCash(paymentId));
    }

    /**
     * 领取现金(带电子签字)
     *
     * @param paymentId         现金发放ID
     * @param receiverSignature 领取人电子签字
     */
    @SaCheckPermission("salary:cash:receive")
    @Log(title = "现金发放", businessType = BusinessType.UPDATE)
    @PutMapping("/receive/signature/{paymentId}")
    public R<Void> receiveCashWithSignature(@PathVariable Long paymentId, @RequestParam String receiverSignature) {
        return toAjax(iCashPaymentService.receiveCashWithSignature(paymentId, receiverSignature));
    }

    /**
     * 领取现金(验证手机号后)
     *
     * @param paymentId         现金发放ID
     * @param receiverSignature 领取人电子签字
     * @param employeeNo        工号
     */
    @SaCheckPermission("salary:cash:receive")
    @Log(title = "现金发放", businessType = BusinessType.UPDATE)
    @PutMapping("/receive/verified/{paymentId}")
    public R<Void> receiveCashWithVerification(@PathVariable Long paymentId,
            @RequestParam String receiverSignature,
            @RequestParam String employeeNo) {
        return toAjax(iCashPaymentService.receiveCashWithVerification(paymentId, receiverSignature, employeeNo));
    }
}
